<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use OT\DataDictionary;
use Think\Page;
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController
{
    //系统首页
    public function index()
    {
        $data = M('Category')->where(array('title' => '今日推荐'))->field('id')->select();
        $todayid = $data[0]['id'];
        $data = M('Category')->where(array('title' => '明日推荐'))->field('id')->select();
        $tomorrowid = $data[0]['id'];
        $today = M('Document')->where(array('category_id' => $todayid, 'status' => 1))->order('update_time desc')->limit(10)->select();
        $tomorrow = M('Document')->where(array('category_id' => $tomorrowid, 'status' => 1))->order('update_time desc')->limit(10)->select();

        $demand = D('MainInfo')->where(array('type' => '需求方', 'finish' => 0, 'show'=> 1))->order('modify_time desc')->limit(10)->select();
        $supply = D('MainInfo')->where(array('type' => '供应方', 'finish' => 0, 'show'=> 1))->order('modify_time desc')->limit(10)->select();
        $deal = D('MainInfo')->where(array('finish' => 1, 'show'=> 1))->order('modify_time desc')->limit(10)->select();
        $picture = D('Carousel')->where(array('show' => '1'))->select();
//        $video = D('Video')->where(array('show'=>'1'))->limit(3)->select();
        $server = D('Server')->where(array('show' => '1'))->limit(1)->select();

        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);
        //广告
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();

//        微博
        $weibo = M('Url')->find();

        $wechat = M('Wechat')->find();

        $this->assign('wechat',$wechat);
        $this->assign('weibo', $weibo);

        $this->assign('advs', $advs);
        $this->assign('server', $server);
        $this->assign('today', $today);
        $this->assign('tomorrow', $tomorrow);
        $this->assign('demand', $demand);
        $this->assign('supply', $supply);
        $this->assign('deal', $deal);
        $this->assign('picture', $picture);
//        $this->assign('video',$video);

        $this->display();
    }

    public function about()
    {

        $server = D('Server')->limit(1)->select();
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs', $advs);
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $about = D('About')->order('post_time desc')->find();
        $this->assign('link', $link);
        $this->assign('server', $server);
        $this->assign('about',$about);
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        $this->display();
    }

    public function offer()
    {
        $server = D('Server')->limit(1)->select();
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs', $advs);
        $this->assign('server', $server);
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        $this->display();
    }

    public function partner()
    {
        $type = I('type','1');
        $partner = M('Partner')->where(array('type'=>$type))->select();
        $size = sizeof($partner) / 5;

        switch ($type){
            case 1: $title="优质吊装物流供应商"; break;
            case 2: $title="诚信合作客户"; break;
            case 3: $title="行业相关领域"; break;
            default: $title="优质吊装物流供应商";
        }

        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs', $advs);
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);
        $this->assign('size', ceil($size));
        $this->assign('partner', $partner);
        $this->assign('title', $title);
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        $this->display();
    }


    public function tousu_add()
    {
        $advice = I('advice', '');
        $create_time = date('Y-m-d H:i:s');
        if ($advice != '' && trim($advice) != '') {
            $data['advice'] = $advice;
            $data['create_time'] = $create_time;
            $res = M('Advices')->data($data)->add();
            if ($res) {
                $this->ajaxReturn(array('status' => 1, 'info' => '提交成功!'), 'JSON');
            } else {
                $this->ajaxReturn(array('status' => 0, 'info' => '提交失败!'), 'JSON');
            }
        } else {
            $this->ajaxReturn(array('status' => 0, 'info' => '提交失败!'), 'JSON');
        }
    }

    public function help()
    {
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $help = D('Help')->order('post_time desc')->find();
        $this->assign('help',$help);
        $this->assign('advs', $advs);
        $this->display();
    }

    public function recruit($p = 1)
    {
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        $server = D('Server')->limit(1)->select();
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $where['show'] = 1;
        $recruit = M('Recruit');
        $page_size = 20;
        $count = $recruit->where($where)->count();
        $list = $recruit->where($where)->page($p . ',' . $page_size)->order('create_time desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('advs', $advs);
        $this->assign('server', $server);
        $this->assign('recruit', $list);
        $this->display();
    }

    public function rec_detail($id)
    {
        if ($id == null) {
            $this->error('文档id错误');
        } else {
            //        微博
            $weibo = M('Url')->find();
            $this->assign('weibo', $weibo);
            $recruit = D('Recruit')->where(array('id' => $id))->find();
            $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
            /** 友情链接调用**/
            $link = D('Link')->order('create_time desc')->select();
            $this->assign('link', $link);
            $this->assign('advs', $advs);
            $this->assign('recruit', $recruit);
            $this->display();
        }
    }

    public function second($p = 1)
    {
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        $server = D('Server')->limit(1)->select();
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $where['show'] = 1;
        $second = M('Seconds');
        $page_size = 20;
        $count = $second->where($where)->count();
        $list = $second->page($p . ',' . $page_size)->where($where)->order('create_time desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('advs', $advs);
        $this->assign('server', $server);
        $this->assign('second', $list);
        $this->display();
    }

    public function se_detail($id)
    {
        if ($id == null) {
            $this->error('文档id错误');
        } else {
            //        微博
            $weibo = M('Url')->find();
            $this->assign('weibo', $weibo);
            $second = D('Seconds')->where(array('id' => $id))->find();
            $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
            /** 友情链接调用**/
            $link = D('Link')->order('create_time desc')->select();
            $this->assign('link', $link);
            $this->assign('advs', $advs);
            $this->assign('second', $second);
            $this->display();
        }
    }

    public function album(){
        /** 客服 **/
        $server = D('Server')->limit(1)->select();
        $this->assign('server', $server);

        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);

        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);

        /** 广告 **/
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs', $advs);

        $album = M('Album')->where(array('status'=>'1'))->select();
        $size = sizeof($album)/3;
        $this->assign('size',ceil($size));
        $this->assign('list',$album);
        $this->display();
    }

    public function showAlbum($id = null)
    {
        /** 客服 **/
        $server = D('Server')->limit(1)->select();
        $this->assign('server', $server);

        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);

        /** 广告 **/
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs', $advs);

        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);

        if ($id == null) {
            $this->error('图册id错误');
        } else {
            $album = M('Album')->where(array('id' => $id))->find();
            if ($album==null){
                $this->error("信息错误!");
            }else {
                $this->assign('info', $album);
                $this->display();
            }
        }
    }

}